"user strict";
//品牌
const Controller = require('egg').Controller;
class PingpaiController extends Controller{
    async index() {
        const { ctx,app } = this;
        const { pingpai } =ctx.request.query;
        let data;
        if(pingpai){
            data = await app.mysql.query(`select id,pingpai from recommend where pingpai like '%${keyname}%'`);
        }else{
            data = await app.mysql.query(`select id,pingpai from recommend`);
        }
        if(data){
            ctx.body = {
                code:0,
                msg:'查询成功',
                data
            }
        }else{
            ctx.body = {
                code:4004,
                msg:'查询失败',
            }
        }
        
    }
    async create() {
        const { ctx,app } = this;
        const { pingpai } =ctx.request.body;
        if(pingpai){
            const result = await app.mysql.insert('recommend',{pingpai});
            ctx.body = {
                code:0,
                msg:'新增成功',
                data:result
            }
        }else{
            ctx.body = {
                code:4001,
                msg:'新增失败'
            }
        }
    }
    async update() {
        const { ctx,app } = this;
        const {
            pingpai
        } = ctx.request.body;//传进来的数据
        const {
            id
        } = ctx.params;
        const row = {
            pingpai:pingpai,
        };
        const options = { //通过这个id 来选定的修改数据
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('recommend', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: "编辑成功",
            };
        } else {
            ctx.body = {
                code: 4008,
                msg: "编辑失败",
            };
        }
    
    }
     //删除
     async destroy() { //DELETE
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.delete(`recommend`, {
            id: +id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            };
        }
    }
}
module.exports = PingpaiController;